<template>
  <div class="ng-pagination">
    <slot name="prefix"></slot>
    <el-pagination
      background
      :current-page.sync="privatePageNo"
      :page-sizes="pageSizes"
      :page-size.sync="privatePageSize"
      :total.sync="privateTotalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      class="el-pagination__colorful"
      layout="sizes,prev, pager, next, total, jumper"
    />
    <slot name="after"></slot>
  </div>
</template>

<script>
export default {
  name: "NG-pagination",
  props: {
    pageNo: {
      type: Number,
      default: 0,
    },
    pageSize: {
      type: Number,
      default: 0,
    },
    totalCount: {
      type: Number,
      default: 0,
    },
    pageSizes: {
      type: Array,
      default: () => [5, 10, 15, 20, 25, 30],
    },
  },
  computed: {
    privatePageNo: {
      get() {
        return this.$props.pageNo;
      },
      set(val) {
        this.$emit("update:pageNo", val);
      },
    },
    privatePageSizes: {
      get() {
        return this.$props.pageSizes;
      },
      set(val) {
        this.$emit("update:pageSizes", val);
      },
    },
    privatePageSize: {
      get() {
        return this.$props.pageSize;
      },
      set(val) {
        this.$emit("update:pageSize", val);
      },
    },
    privateTotalCount: {
      get() {
        return this.$props.totalCount;
      },
      set(val) {
        this.$emit("update:totalCount", val);
      },
    },
  },
  methods: {
    handleSizeChange(val) {
      this.$emit("on-size-change", val);
      this.$emit("on-change", {
        pageNo: this.privatePageNo,
        pageSize: this.privatePageSize,
        totalCount: this.privateTotalCount,
      });
    },
    handleCurrentChange(val) {
      this.$emit("on-current-change", val);
      this.$emit("on-change", {
        pageNo: this.privatePageNo,
        pageSize: this.privatePageSize,
        totalCount: this.privateTotalCount,
      });
    },
  },
};
</script>

<style lang="less" scoped>
.ng-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
